package services.impl;

import services.*;
import entities.*;
import java.util.List;
import java.util.LinkedList;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.function.Predicate;
import java.util.Arrays;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;
import org.apache.commons.lang3.SerializationUtils;
import java.util.Iterator;

public class CreateDailyServiceImpl implements CreateDailyService, Serializable {
	
	
	public static Map<String, List<String>> opINVRelatedEntity = new HashMap<String, List<String>>();
	
	
	ThirdPartyServices services;
			
	public CreateDailyServiceImpl() {
		services = new ThirdPartyServicesImpl();
	}

	
	//Shared variable from system services
	
	/* Shared variable from system services and get()/set() methods */
			
	/* all get and set functions for temp property*/
				
	
	
	/* Generate inject for sharing temp variables between use cases in system service */
	public void refresh() {
		LabManageSystem labmanagesystem_service = (LabManageSystem) ServiceManager.getAllInstancesOf("LabManageSystem").get(0);
	}
	
	/* Generate buiness logic according to functional requirement */
	@SuppressWarnings("unchecked")
	public boolean createDaily(String id, AffairType type, String groupId, String studentId, String content) throws PreconditionException, PostconditionException, ThirdPartyServiceException {
		
		
		/* Code generated for contract definition */
		//Get daily
		Daily daily = null;
		//no nested iterator --  iterator: any previous:any
		for (Daily d : (List<Daily>)EntityManager.getAllInstancesOf("Daily"))
		{
			if (d.getId().equals(id))
			{
				daily = d;
				break;
			}
				
			
		}
		//Get sig
		StudentInGroup sig = null;
		//no nested iterator --  iterator: any previous:any
		for (StudentInGroup s : (List<StudentInGroup>)EntityManager.getAllInstancesOf("StudentInGroup"))
		{
			if (s.getGroupId().equals(groupId) && s.getStudentId().equals(studentId))
			{
				sig = s;
				break;
			}
				
			
		}
		/* previous state in post-condition*/
 
		/* check precondition */
		if (StandardOPs.oclIsundefined(daily) == true && StandardOPs.oclIsundefined(sig) == false) 
		{ 
			/* Logic here */
			Daily d = null;
			d = (Daily) EntityManager.createObject("Daily");
			d.setId(id);
			d.setBelongUser(studentId);
			d.setBelongGroup(groupId);
			d.setAffair(type);
			d.setContent(content);
			d.setAgree(false);
			d.setFinish(false);
			
			
			refresh();
			// post-condition checking
			if (!(true && 
			d.getId() == id
			 && 
			d.getBelongUser() == studentId
			 && 
			d.getBelongGroup() == groupId
			 && 
			d.getAffair() == type
			 && 
			d.getContent() == content
			 && 
			d.getAgree() == false
			 && 
			d.getFinish() == false
			 && 
			true)) {
				throw new PostconditionException();
			}
			
		
			//return primitive type
			refresh();				
			return true;
		}
		else
		{
			throw new PreconditionException();
		}
		//string parameters: [id, groupId, studentId, content] 
		//all relevant vars : d
		//all relevant entities : Daily
	}  
	
	static {opINVRelatedEntity.put("createDaily", Arrays.asList("Daily"));}
	 
	@SuppressWarnings("unchecked")
	public boolean editDaily(String id, AffairType type, String content) throws PreconditionException, PostconditionException, ThirdPartyServiceException {
		
		
		/* Code generated for contract definition */
		//Get daily
		Daily daily = null;
		//no nested iterator --  iterator: any previous:any
		for (Daily d : (List<Daily>)EntityManager.getAllInstancesOf("Daily"))
		{
			if (d.getId().equals(id))
			{
				daily = d;
				break;
			}
				
			
		}
		/* previous state in post-condition*/
 
		/* check precondition */
		if (StandardOPs.oclIsundefined(daily) == false) 
		{ 
			/* Logic here */
			daily.setAffair(type);
			daily.setContent(content);
			
			
			refresh();
			// post-condition checking
			if (!(daily.getAffair() == type
			 && 
			daily.getContent() == content
			 && 
			true)) {
				throw new PostconditionException();
			}
			
		
			//return primitive type
			refresh();				
			return true;
		}
		else
		{
			throw new PreconditionException();
		}
		//string parameters: [id, content] 
		//all relevant vars : daily
		//all relevant entities : Daily
	}  
	
	static {opINVRelatedEntity.put("editDaily", Arrays.asList("Daily"));}
	 
	@SuppressWarnings("unchecked")
	public boolean finishDaily(String id) throws PreconditionException, PostconditionException, ThirdPartyServiceException {
		
		
		/* Code generated for contract definition */
		//Get daily
		Daily daily = null;
		//no nested iterator --  iterator: any previous:any
		for (Daily d : (List<Daily>)EntityManager.getAllInstancesOf("Daily"))
		{
			if (d.getId().equals(id))
			{
				daily = d;
				break;
			}
				
			
		}
		/* previous state in post-condition*/
 
		/* check precondition */
		if (StandardOPs.oclIsundefined(daily) == false) 
		{ 
			/* Logic here */
			daily.setFinish(true);
			
			
			refresh();
			// post-condition checking
			if (!(daily.getFinish() == true
			 && 
			true)) {
				throw new PostconditionException();
			}
			
		
			//return primitive type
			refresh();				
			return true;
		}
		else
		{
			throw new PreconditionException();
		}
		//string parameters: [id] 
		//all relevant vars : daily
		//all relevant entities : Daily
	}  
	
	static {opINVRelatedEntity.put("finishDaily", Arrays.asList("Daily"));}
	 
	
	
	
	/* temp property for controller */
			
	/* all get and set functions for temp property*/
	
	/* invarints checking*/
	public final static ArrayList<String> allInvariantCheckingFunction = new ArrayList<String>(Arrays.asList());
			
}
